package com.msconfig.mvp.view.activity.base;

/*
 * author: caojunhao
 * date  : 2020/4/28
 * desc  :
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.geo.msconfig.R;
import com.msconfig.manager.DefaultFragmentTransaction;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

//初始化Presenter与VIEW绑定
public abstract class BaseMvpActivity extends BaseActivity implements BaseView{

    public List<BasePresenter> mPresenters;
    protected BaseMvpFragment mShowFragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPresenter();
        initView();
        initData();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

    }


    private void initPresenter() {
        //初始化Presenter
        if (mPresenters == null) {
            mPresenters = new ArrayList<>();
        }
        createPresenter(mPresenters);
        //presenter与View绑定
        if (null != mPresenters) {
            for (BasePresenter presenter : mPresenters) {
                presenter.attachView(this);
            }
        }
    }

    @Override
    public void onDestroy() {
        detachPresenter();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    private void detachPresenter() {
        //presenter与activity解绑定
        if (null != mPresenters) {
            while (!mPresenters.isEmpty()) {
                BasePresenter presenter = mPresenters.get(0);
                presenter.detachView();
                mPresenters.remove(0);
            }
        }
    }

    protected abstract void createPresenter(List<BasePresenter> mPresenters);

    /**
     * fragment跳转
     * 其实就是相当于FragmentTransaction.replace(containerViewId,fragment);
     * replace方法就等效于对这个Fragment先执行remove()，再执行add()
     * 使用replace和add都会重新执行fragmenta生命方法
     * addToBackStack--把当前fragment添加到回退栈中
     * containerViewId--要替换的容器的id
     * fragment--即将替换的fragment
     */
    public void goToNextFragment(BaseMvpFragment hideFragment, BaseMvpFragment showFragment){
        this.mShowFragment = showFragment;
        new DefaultFragmentTransaction(getSupportFragmentManager())
                .hide(hideFragment)//先隐藏上一个fragment
                .showSingleView(R.id.container, showFragment)//需不需要把这个fragment先保存起来，防止多次加载数据
                .addToBackStack(showFragment.getClass().getName())//把当前fragment保存到栈中
                .doAndAddToBackStack();//再次判断是否允许堆栈操作，保存数据
    }

    public void goToNextFragment(BaseMvpFragment showFragment){
        this.mShowFragment = showFragment;
        new DefaultFragmentTransaction(getSupportFragmentManager())
                .replace(R.id.container, showFragment)//先隐藏上一个fragment
                .addToBackStack(showFragment.getClass().getName())//把当前fragment保存到栈中
                .doAndAddToBackStack();//再次判断是否允许堆栈操作，保存数据
    }

    @Override
    public void showToast(int resId) {
        Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
    }

}
